package com.dang.alex.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.dang.alex.dao.daoInterface.OperationInterface;
import com.dang.alex.model.User;
import com.dang.alex.util.JdbcUtil;
/**
 * 连接数据库并发送SQL语句
 * @author Administrator
 *
 */
public class OperationUser implements OperationInterface{
	
	Connection conn=null;
	PreparedStatement stmt=null;
	ResultSet rs=null;
	User user=null;
	
	
	private static final String INSERT= "insert into d_user(email,nickname,password,user_integral,is_email_verify,email_verify_code,last_login_time,last_login_ip) values(?,?,?,?,?,?,?,?)";
	private static final String FINDBYEMAIL= "select * from d_user where email=?";
	private static final String FINDBYID="select * from d_user where id=?";
	private static final String UPDATE= "update   d_user   set email=?,nickname=?,password=?,user_integral=?,is_email_verify=?,email_verify_code=?,last_login_time=?,last_login_ip=? where id=?";
  
   
    
    /**
     * 通过email从数据库中查找对应的User
     * @param email
     * @return
     * @throws Exception
     */
	public User findByEmail(String email) throws Exception {
    	PreparedStatement prep= null;
    	Connection conn= null;
    	User u= null;ResultSet rst= null;
    	try{conn= JdbcUtil.getConnection();
    	prep= conn.prepareStatement(FINDBYEMAIL);
    	prep.setString(1, email);rst= prep.executeQuery();
    	if(rst.next()) {
    		u= new User();
    		u.setEmail(rst.getString("email"));
    		u.setEmailVerifyCode(rst.getString("email_verify_code"));
    		u.setId(rst.getInt("id"));
    		u.setLast_login_ip(rst.getString("last_login_ip"));
    		u.setLast_login_time(rst.getLong("last_login_time"));
    		u.setNickname(rst.getString("nickname"));
    		u.setPassword(rst.getString("password"));
    		u.setUser_integral(rst.getInt("user_integral"));
    		String is_email_verify= rst.getString("is_email_verify");
    		if(is_email_verify.equals("T")) {u.setIs_email_verify(true);} 
    		else{u.setIs_email_verify(false);}}} finally{JdbcUtil.close(prep, conn);}
    	return u;
    	}

	/**
	 *通id值来查找对应的User
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public User findById(int id) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		User u = null;
		try {
			conn = JdbcUtil.getConnection();
			prep = conn.prepareStatement(FINDBYID);
			prep.setInt(1, id);
			ResultSet rst = prep.executeQuery();
			if (rst.next()) {
				u = new User();
				u.setEmail(rst.getString("email"));
				u.setEmailVerifyCode(rst.getString("email_verify_code"));
				u.setId(rst.getInt("id"));
				u.setLast_login_ip(rst.getString("last_login_ip"));
				u.setLast_login_time(rst.getLong("last_login_time"));
				u.setNickname(rst.getString("nickname"));
				u.setPassword(rst.getString("password"));
				u.setUser_integral(rst.getInt("user_integral"));
				String is_email_verify = rst.getString("is_email_verify");
				if (is_email_verify.equals("T")) {
					u.setIs_email_verify(true);
				} else {
					u.setIs_email_verify(false);
				}
			}
		} finally {
			JdbcUtil.close(prep, conn);
		}
		return u;
	}

	/**
	 * 保存User到数据库
	 * @param user
	 * @throws Exception
	 */
	public void save(User user) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
			prep.setString(1, user.getEmail());
			prep.setString(2, user.getNickname());
			prep.setString(3, user.getPassword());
			prep.setInt(4, user.getUser_integral());
			if (user.isIs_email_verify()) {
				prep.setString(5, "T");
			} else {
				prep.setString(5, "F");
			}
			prep.setString(6, user.getEmailVerifyCode());
			prep.setLong(7, user.getLast_login_time());
			prep.setString(8, user.getLast_login_ip());
			prep.executeUpdate();
			ResultSet rst = prep.getGeneratedKeys();
			rst.next();
			int id = rst.getInt(1);
			user.setId(id);
		} finally {
			JdbcUtil.close(prep, conn);
		}
	}

	/**
	 * 更新User
	 * @param u
	 * @throws Exception
	 */
	public void update(User u) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			prep = conn.prepareStatement(UPDATE);
			prep.setString(1, u.getEmail());
			prep.setString(2, u.getNickname());
			prep.setString(3, u.getPassword());
			prep.setInt(4, u.getUser_integral());
			if (u.isIs_email_verify()) {
				prep.setString(5, "T");
			} else {
				prep.setString(5, "F");
			}
			prep.setString(6, u.getEmailVerifyCode());
			prep.setLong(7, u.getLast_login_time());
			prep.setString(8, u.getLast_login_ip());
			prep.setInt(9, u.getId());
			prep.executeUpdate();
		} finally {
			JdbcUtil.close(prep, conn);
		}
	}

}
